<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="时间" prop="time">
        <el-date-picker
          v-model="queryParams.time"
          type="date"
          placeholder="请选择时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="分类" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择分类" clearable size="small">
          <el-option value="guard" label="安保排班"/>
          <el-option value="staff" label="行政排班"/>
          <el-option value="school" label="填报"/>
        </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="currentData()" style="color: #333333"
              :header-cell-style="{background:'#E3E3E3'}">
      <el-table-column show-overflow-tooltip label="标题名称" align="center" prop="title"/>
      <el-table-column label="类别" align="center" prop="type">
        <template v-slot="scope">
          <span>{{ switchStr(scope.row.type) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="时间" align="center" prop="time">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button size="mini" type="text" @click="toLineRecordList(scope.row)">查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
  </div>
</template>

<script>
import {
  parseTime
} from "@/utils/ruoyi";

import {listUndoneSchedule} from "@/api/patrol/statistics";
import {listFormNoPage} from "@/api/patrol/form";
import {getDailyformInt} from "@/api/patrol/daily";
import {getSchoolCount} from "@/api/patrol/school";
import {total} from "@/api/patrol/reporting";

export default {
  name: "more",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        time: null,
        type: null,
        title: null
      },
      schoolCount: 0,
      chooseDate: null,
      lineNoDos: [],
      backlogData: [],
      backlogBackUp: [],
      formNoFills: [],
      securityRisks: {},
      dailyFormData: 0
    };
  },
  computed: {},
  created() {
    this.getSchoolCount();
    this.getList();
  },
  watch: {},
  methods: {
    getSchoolCount() {
      getSchoolCount().then(response => {
        this.schoolCount = response.schoolCount;
      });
    },
    getTimeType(data) {
      if (data.updateTime == null) {
        if (new Date(data.completetime).getTime() < new Date().getTime()) {
          return true;
        }
      } else if (new Date(data.completetime).getTime() < new Date(data.updateTime).getTime()) {
        return true;
      }
    },
    /** 查询安全隐患上报列表 */
    async getList() {
      this.loading = true;
      this.backlogData = [];
      // await this.getUndoList();
      await listUndoneSchedule().then(rows => {
        console.log("1:", rows)
        this.lineNoDos = rows;
        if (rows && rows.length > 0) {
          rows.forEach(item => {
            this.backlogData.push({
              title: item.memberName + '未提交安全巡查记录',
              time: item.createTime,
              type: item.type,
              schoolName: item.schoolName,
              lineName: item.lineName,
              memberName: item.memberName,
              scheduleId: item.scheduleId
            })
          })
        }
      });
      // await this.getFormNoFills();
      await listFormNoPage().then(rows => {
        console.log("2:", rows)
        this.formNoFills = rows;
        if (rows && rows.length > 0) {
          rows.forEach(item => {
            this.backlogData.push({
              title: item.formName + '有' + item.fillingX + '个学校未提交',
              time: item.createTime,
              id: item.id,
              type: 'school'
            })
          })
        }
      });
      // await this.getTotal();
      await total().then(require => {
        this.securityRisks = require.data
        console.log("3:", require.data)
        this.backlogData.push({
          title: '区域有' + (this.securityRisks.total - this.securityRisks.complete) + '个安全隐患未处理',
          time: parseTime(new Date()),
          type: 'safe'
        })
      })
      // await this.getDailyFormInt();
      await getDailyformInt().then(rows => {
        console.log("4:", rows)
        this.dailyFormData = rows;
        this.backlogData.push({
          title: '每日校园安全检查、整改记录表有' + (this.schoolCount - rows) + '个学校未提交',
          time: parseTime(new Date()),
          type: 'today'
        })
        // this.total = this.backlogData.length
        this.backlogData.sort(function (a, b) {
          return new Date(b.time) - new Date(a.time);
        });
        this.backlogBackUp = this.backlogData
        this.backlogData = this.backlogBackUp.filter(item => {
          return (!this.queryParams.time || parseTime(item.time, '{y}-{m}-{d}') === parseTime(this.queryParams.time, '{y}-{m}-{d}')) &&
            (!this.queryParams.type || item.type === this.queryParams.type) &&
            (!this.queryParams.title || item.title.includes(this.queryParams.title));
        });
        this.total = this.backlogData.length
      });
      this.loading = false;
    },
    // 当前页的数据
    currentData() {
      const start = (this.queryParams.pageNum - 1) * this.queryParams.pageSize;
      const end = start + this.queryParams.pageSize;
      return this.backlogData.slice(start, end);
    },
    switchStr(type) {
      switch (type) {
        case 'guard':
          return "安保排班";
        case 'staff':
          return "行政排班";
        case 'school':
          return "表单";
        case 'today':
          return "每日填报";
        case 'safe':
          return "安全隐患";

      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.backlogData = []
      this.backlogData = this.backlogBackUp.filter(item => {
        return (!this.queryParams.time || parseTime(item.time, '{y}-{m}-{d}') === parseTime(this.queryParams.time, '{y}-{m}-{d}')) &&
          (!this.queryParams.type || item.type === this.queryParams.type) &&
          (!this.queryParams.title || item.title.includes(this.queryParams.title));
      });
      this.total = this.backlogData.length
      // this.currentData()

    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 跳转按钮操作 */
    toLineRecordList(item) {
      switch (item.type) {
        case 'guard':
          this.$router.push({
            path: '/patrol/detail',
            query: {
              scheduleId: item.scheduleId,
              lineName: item.lineName,
              schoolName: item.schoolName,
              memberName: item.memberName
            },
          })
          break;
        case 'staff':
          this.$router.push({
            path: '/patrol/record/staff',
            query: {
              scheduleId: item.scheduleId,
              lineName: item.lineName,
              schoolName: item.schoolName,
              memberName: item.memberName
            },
          })
          break;
        case 'school':
          this.$router.push("/form/viewschool/" + item.id);
          break;
        case 'today':
          this.$router.push({path: '/form/daily/5'})
          break;
        case 'safe':
          this.$router.push({path: '/patrol/reporting'})
          break;
        default :
          break
      }
    }
  }
};
</script>
